﻿Imports Infragistics.Web.UI.NavigationControls
Imports Infragistics.WebUI.UltraWebNavigator
Imports Infragistics.Web.UI.GridControls
Imports Infragistics.Web.UI.ListControls


Public Class OrganizationsUI
    Inherits System.Web.UI.Page
    Public ParentID As Integer
    Private et As New ETModelContainer
    Private Unit As Organization
    Private CurrentUnitMemberID As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not SelectedUnit Is Nothing Then

            '  grdRoles.DataBind()

        End If
    End Sub
    Private Sub PopulateReqs()
        Dim ar = From a In et.AwardRequirements Where a.ParentID Is Nothing
        chkReqs.Nodes.Clear()

        For Each aw As AwardRequirement In ar
            Dim node As Node = New Node()
            node.Text = aw.Requirement
            node.Tag = aw.RequirementID

            node.CheckBox = CheckBoxes.True

            Dim orq = (From x In et.OrgReqs Where x.OrganizationID = SelectedUnit.OrganizationID And x.RequirementID = aw.RequirementID)

            If Not orq.SingleOrDefault Is Nothing Then
                node.Checked = True
            End If
            chkReqs.Nodes.Add(node)


        Next


    End Sub
    Private ReadOnly Property SelectedUnit As Organization
        Get
            If Unit Is Nothing Then
                Unit = GetSelectedUnit()
                If Unit Is Nothing Then Return Nothing
                If Unit.ParentID Is Nothing Then
                    Unit.ParentID = 0
                End If
            End If
            If Unit Is Nothing Then
                Return Nothing
            End If
            ParentID = CInt(Unit.ParentID)
            Return Unit
        End Get
    End Property



    Private Sub trvOrgs_SelectionChanged(sender As Object, e As Infragistics.Web.UI.NavigationControls.DataTreeSelectionEventArgs) Handles trvOrgs.SelectionChanged

        If Not SelectedUnit Is Nothing Then


            txtUnitName.Text = SelectedUnit.OrganizationName
            cboUnitType.SelectedIndex = cboUnitType.Items.IndexOf(cboUnitType.Items.FindByValue(Unit.OrgType.ToString))
            PopulateReqs()

            hdnOrgID.Value = SelectedUnit.OrganizationID.ToString

            grdRoles.DataBind()


        End If


    End Sub




    Private Function GetSelectedUnit() As Organization
        Dim Unit As Organization = Nothing
        If trvOrgs.SelectedNodes.Count > 0 Then
            Dim node As DataTreeNode = trvOrgs.SelectedNodes.First

            Unit = (From o In et.Organizations Where o.OrganizationID = CInt(node.Value)).SingleOrDefault
        End If
        Return Unit
    End Function



    Protected Sub cmdAdd_Click(sender As Object, e As EventArgs) Handles cmdAdd.Click

        Dim trvNode As New DataTreeNode
        trvNode.Text = "New Unit"

        Dim Org As New Organization

        Org.OrganizationName = "New Unit"

        Dim Selected As Organization = GetSelectedUnit()

        If Selected Is Nothing Then
            Org.ParentID = 0
        Else
            Org.ParentID = Selected.OrganizationID
        End If

        et.AddToOrganizations(Org)
        et.SaveChanges()
        trvNode.Value = (Org.OrganizationID).ToString
        trvOrgs.SelectedNodes.First.Nodes.Add(trvNode)
        trvNode.ExpandAnscestors()




    End Sub


    Protected Sub cmdDelete_Click(sender As Object, e As EventArgs) Handles cmdDelete.Click
        Dim Unit As Organization = GetSelectedUnit()
        et.Organizations.DeleteObject(Unit)
        et.SaveChanges()

        trvOrgs.SelectedNodes.First.Remove()


        txtUnitName.Text = String.Empty
        cboUnitType.SelectedIndex = -1
        chkReqs.ClearAll()



    End Sub

    Protected Sub cmdSaveUnit_Click(sender As Object, e As EventArgs) Handles cmdSaveUnit.Click
        Dim org As Organization = GetSelectedUnit()
        org.OrganizationName = txtUnitName.Text
        org.OrgType = CType(cboUnitType.SelectedValue, Integer?)


        trvOrgs.SelectedNodes.First.Text = txtUnitName.Text

        et.SaveChanges()
    End Sub


    Private Sub chkReqs_NodeChecked(sender As Object, e As Infragistics.WebUI.UltraWebNavigator.WebTreeNodeCheckedEventArgs) Handles chkReqs.NodeChecked
        Dim OrgID As Integer = GetSelectedUnit().OrganizationID
        Dim ReqID As Integer = CInt(e.Node.Tag)
        Dim fr = (From x In et.OrgReqs Where x.OrganizationID = OrgID And x.RequirementID = ReqID).SingleOrDefault

        If fr Is Nothing Then
            Dim fc As OrgReq = New OrgReq
            fc.OrganizationID = OrgID
            fc.RequirementID = ReqID

            et.AddToOrgReqs(fc)

        Else
            et.OrgReqs.DeleteObject(fr)
        End If

        et.SaveChanges()
    End Sub

    Private Sub grdRoles_InitializeDefaultEditors(sender As Object, e As Infragistics.Web.UI.GridControls.DefaultEditorsEventArgs) Handles grdRoles.InitializeDefaultEditors

        Dim ddl As WebDropDown = CType(grdRoles.EditorProviders("epUsers").GetEditor, WebDropDown)
        ddl.DataSource = et.ParentOrgUsers(ParentID).ToList
        ddl.TextField = "UserName"
        ddl.ValueField = "UserName"
        ddl.DataBind()
        ' ddl.AutoPostBack = True
        et.Connection.Close()

        Dim rddl As WebDropDown = CType(grdRoles.EditorProviders("epRoles").GetEditor, WebDropDown)
        rddl.DataSource = et.Roles
        rddl.ValueField = "RoleID"
        rddl.TextField = "RoleName"
        rddl.DataBind()
        '  rddl.AutoPostBack = True

    End Sub
 
    Protected Sub cmdAddNewRole_Click(sender As Object, e As EventArgs) Handles cmdAddNewRole.Click

        Dim um As UnitMembership = New UnitMembership
        um.OrganizationID = SelectedUnit.OrganizationID
        um.UserName = User.Identity.Name
        um.UnitRoleID = CInt(My.Settings("UnitReaderRoleID"))
        et.UnitMemberships.AddObject(um)
        et.SaveChanges()
        grdRoles.DataBind()
    End Sub

 
 
    Protected Sub grdRoles_RowUpdating(sender As Object, e As Infragistics.Web.UI.GridControls.RowUpdatingEventArgs) Handles grdRoles.RowUpdating
        Dim Index As Integer = CInt(e.Row.DataKey(0))
        Dim UnitMember As UnitMembership = (From um In et.UnitMemberships Where um.UnitMemberID = Index).First

        UnitMember.UserName = e.Values("UserName").ToString
        UnitMember.UnitRoleID = CInt(e.Values("UnitRoleID"))


        et.SaveChanges()
        grdRoles.DataBind()
    End Sub
End Class
